// TextButton.cpp : implementation file
//

#include "stdafx.h"
#include "calendar.h"
#include "TextButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define	STATE_UP	1
#define	STATE_DOWN	2
#define	STATE_FOCUS	3

/////////////////////////////////////////////////////////////////////////////
// CTextButton

CTextButton::CTextButton()
{
}

CTextButton::~CTextButton()
{
}


BEGIN_MESSAGE_MAP(CTextButton, CButton)
	//{{AFX_MSG_MAP(CTextButton)
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTextButton message handlers

void CTextButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	if(lpDrawItemStruct->itemAction == ODA_FOCUS)
	{
		if(lpDrawItemStruct->itemState & ODS_FOCUS)
		{
			DrawButtonState(lpDrawItemStruct->hDC, STATE_FOCUS); 
		}
		else
		{
			DrawButtonState(lpDrawItemStruct->hDC, STATE_UP); 
		}
	}

	if(lpDrawItemStruct->itemAction == ODA_SELECT)
	{
		if(lpDrawItemStruct->itemState & ODS_SELECTED)
		{
			DrawButtonState(lpDrawItemStruct->hDC, STATE_DOWN); 
		}
		else
		{
			if(lpDrawItemStruct->itemState & ODS_FOCUS)
				DrawButtonState(lpDrawItemStruct->hDC, STATE_FOCUS); 
			else
				DrawButtonState(lpDrawItemStruct->hDC, STATE_UP); 
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////
BOOL CTextButton::DrawButtonState( HDC hDC, UINT uState)
{
	m_uiLastState = uState;

	switch(uState)
	{
	case STATE_UP:
		SetTextColor(hDC, RGB(0,0,0));
		break;
	case STATE_DOWN:
		SetTextColor(hDC, RGB(200,50,0));
		break;
	case STATE_FOCUS:
		SetTextColor(hDC, RGB(100,100,0));
		break;
	default:
		SetTextColor(hDC, RGB(0,0,0));
		m_uiLastState = 0;
//		return FALSE;
	}
	CString txt;

	GetWindowText(txt);
	SetBkColor(hDC, paperColor);

	HGDIOBJ hObj = ::SelectObject(hDC, m_pFont->GetSafeHandle());

	TextOut(hDC, 0,0, txt, txt.GetLength());

	::DeleteObject(hObj);

return TRUE;
}


BOOL CTextButton::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
//	return CButton::OnEraseBkgnd(pDC);
	return TRUE;
}

void CTextButton::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	//HDC hDC = ::GetWindowDC(::GetParent(GetSafeHwnd()));
	//DrawButtonState( hDC, m_uiLastState);	
	DrawButtonState( dc.GetSafeHdc(), m_uiLastState);	
//	GetFont();
	
	// Do not call CButton::OnPaint() for painting messages
}

//////////////////////////////////////////////////////////////////////////////////
BOOL CTextButton::SetWindowText(LPCTSTR lpszString)
{
	if(lpszString == NULL)
		return FALSE;

	CDC *pDC = GetDC();
	pDC->SelectObject(m_pFont);
	CSize	txtSize = pDC->GetTextExtent(lpszString);
	
	SetWindowPos(&wndTop, 0, 0, txtSize.cx, txtSize.cy, SWP_NOMOVE);
	
	CButton::SetWindowText(lpszString);

	return TRUE;
}

void CTextButton::SetFaceFont(CFont *pFont)
{
	m_pFont = pFont;
}
